/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef HI182X_CARD_H
#define HI182X_CARD_H

#include <mc/engine.h>
#include <mc/core/timer.h>
#include <ncsi_over_mctp/ncsi_over_mctp_huawei.h>
#include <smbus/smbus.h>
#include <imu/imu.h>

#include "hi182x_port.h"
#include "interface/board.h"
#include "interface/network_adapter.h"
#include "interface/network_adapter/cooling.h"
#include "interface/network_adapter/fault_status.h"
#include "interface/network_adapter/log_collection.h"
#include "interface/pcie_card.h"
#include "interface/pcie_device.h"
#include "interface/pcie_device/bandwidth.h"

namespace dev {
using ncsi_over_mctp_hw_ptr = std::shared_ptr<ncsi_over_mctp_huawei>;
using smbus_obj_ptr         = mc::shared_ptr<smbus>;
using ref_chip_ptr          = mc::shared_ptr<mc::engine::ref_object>;
using imu_ptr               = std::shared_ptr<imu>;

class hi182x_card : public mc::engine::object<hi182x_card> {
public:
    MC_OBJECT(
        hi182x_card, "PCIeNicCard", "/bmc/dev/Systems/1/PCIeNicCard/${object_name}",
        (PCIeDevice)(PCIeCard)(NetworkAdapter)(NetworkAdapter_FaultStatus)(NetworkAdapter_Cooling)(Board)(PCIeDevice_Bandwidth)(NetworkAdapter_LogCollection))

    hi182x_card();
    ~hi182x_card();
    bool start();
    bool stop();
    bool init(mc::mutable_dict& csr_object, const mc::dict& connector);
    void init_network_ports();
    void update_ports_bdf_async(imu_pcie_device_req_t& pcie_device_info);
    void replace_dynamic_property(mc::mutable_dict& object, const mc::dict& connector);
    bool update_max_sfp_temperature();

    // === smbus ===
    void register_RefChip();
    void register_WriteRead_method(
        std::function<std::vector<uint8_t>(const std::vector<uint8_t>&, uint32_t)> func);

    PCIeDevice                   m_pcie_device;
    PCIeCard                     m_pcie_card;
    NetworkAdapter               m_network_adapter;
    NetworkAdapter_FaultStatus   m_network_adapter_fault_status;
    NetworkAdapter_Cooling       m_network_adapter_cooling;
    Board                        m_board;
    PCIeDevice_Bandwidth         m_pcie_device_bandwidth;
    NetworkAdapter_LogCollection m_network_adapter_log_collection;
    uint8_t                      m_system_id;

private:
    std::vector<hi182x_port*> m_network_ports;

    // ncsi over mctp 实现
private:
    bool                  start_ncsi_protocol();
    void                  stop_ncsi_protocol();
    void                  handle_os_reset();
    bool                  start_protocol();
    bool                  start_smbus_protocol();
    bool                  init_smbus_protocol();
    bool                  init_imu_protocol();
    ref_chip_ptr          m_ref_chip;
    smbus_obj_ptr         m_smbus_obj;
    imu_ptr               m_imu_obj;
    ncsi_over_mctp_hw_ptr m_ncsi_over_mctp_huawei;
    mc::milliseconds      m_interval       = mc::milliseconds(5000); // ncsi over mctp 更新间隔5s
    mc::milliseconds      m_smbus_interval = mc::milliseconds(5000); // smbus 更新间隔5s
    mctp*                 m_mctp_object; // 管理MCTP对象生命周期，防止内存泄漏
};

} // namespace dev

#endif // __HI182X_CARD_H__
